<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">  
var $ = require(&#39;jquery&#39;),
  BUI = require(&#39;bui-common&#39;),
  Util = require(&#39;./util&#39;);

<span id='BUI-Graphic-Base'>/**
</span> * @class BUI.Graphic.Base
 * 图形控件或者分组的基类
 */
var Base = function(cfg){
  this.cfg = cfg;
  this._attrs = {
    autoRender : true,
          visible : true
  };
  var defaultCfg = this.getDefaultCfg();
      BUI.mix(this._attrs,defaultCfg,cfg);
      if(this.get(&#39;autoRender&#39;)){
        this.render();
      }
      
};

Base.ATTRS = {
<span id='BUI-Graphic-Base-property-zIndex'>      /**
</span>       * 所在父元素中的优先级，仅在父元素排序时有效
       * @type {Number}
       */
  zIndex : {

      },
<span id='BUI-Graphic-Base-property-el'>  /**
</span>   * @protected
   * 底层使用了raphael 所以此属性对应raphael的对对象
   */
  el : {},
<span id='BUI-Graphic-Base-property-node'>  /**
</span>   * svg或者vml对象
   * @type {HTMLElement}
   */
  node : {},
<span id='BUI-Graphic-Base-property-canvas'>      /**
</span>       * 画布
       * @type {BUI.Graphic.Canvas}
       */
      canvas : {

      },
<span id='BUI-Graphic-Base-property-visible'>      /**
</span>       * 是否显示
       * @type {Boolean}
       */
      visible : {
          value : true
      }
};

BUI.augment(Base,{

<span id='BUI-Graphic-Base-method-getDefaultCfg'>  /**
</span>   * 获取默认的配置信息
   * @return {Object} 默认的属性
   */
  getDefaultCfg : function(){
    return {};
  },
<span id='BUI-Graphic-Base-method-set'>  /**
</span>   * 设置属性信息
   * @protected
   */
  set : function(name,value){
    this._attrs[name] = value;
  },
<span id='BUI-Graphic-Base-method-get'>  /**
</span>   * 获取属性信息
   * @protected
   */
  get : function(name){
    return this._attrs[name];
  },
<span id='BUI-Graphic-Base-method-getCfgAttr'>  /**
</span>   * 获取初始配置的信息
   * @param  {String} name 配置项名称
   * @return {*}  初始值
   */
  getCfgAttr : function(name){
      return this.cfg[name];

  },
<span id='BUI-Graphic-Base-method-show'>  /**
</span>   * 显示
   */
  show : function(){
      this.get(&#39;el&#39;).show();
      this.set(&#39;visible&#39;,true);
  },
<span id='BUI-Graphic-Base-method-hide'>  /**
</span>   * 隐藏
   */
  hide : function(){
      this.get(&#39;el&#39;).hide();
      this.set(&#39;visible&#39;,false);
  },  
<span id='BUI-Graphic-Base-method-attr'>  /**
</span>   * 设置或者设置属性，有一下3中情形：
   *
   *   - name为字符串，value 为空，获取属性值
   *   - name为字符串，value不为空，设置属性值，返回this
   *   - name为键值对，value 为空，设置属性值
   *   
   * @param  {String|Object} name  属性名
   * @param  {*} value 属性值
   * @return {*} 属性值
   */
  attr : function(name,value){
    var _self = this,
      el = _self.get(&#39;el&#39;);
    if(BUI.isObject(name)){
      BUI.each(name,function(v,k){
        _self.attr(k,v);
      });
      return _self;
    }
    if(value !== undefined){
      return _self._setAttr(name,value);
    }
    return _self._getAttr(name);
  },
<span id='BUI-Graphic-Base-method-on'>  /**
</span>   * 附加事件
   * @param  {String}   eventType 事件类型
   * @param  {Function} fn  事件处理函数
   */
  on : function(eventType,fn){
    var _self = this,
      node = _self.get(&#39;node&#39;);
    $(node).on(eventType,fn);
    return this;
  },
<span id='BUI-Graphic-Base-method-off'>  /**
</span>   * 移除事件
   * @param  {String}   eventType 事件类型
   * @param  {Function} fn  事件处理函数
   */
  off : function(eventType,fn){
    var _self = this,
      node = _self.get(&#39;node&#39;);
    $(node).off(eventType,fn);
    return this;
  },
<span id='BUI-Graphic-Base-method-fire'>  /**
</span>   * 触发事件
   * @param  {String} eventType 事件类型
   */
  fire : function(eventType){
    var _self = this,
      node = _self.get(&#39;node&#39;);
    $(node).trigger(eventType);
  },
<span id='global-method-'>  /**
</span>   * 添加委托事件,ie7下无效
   * @param  {String}   selector  选择器
   * @param  {String}   eventType 事件类型
   * @param  {Function} fn  事件处理函数
   * @ignore
  delegate : function(selector,eventType,fn){
    var _self = this,
      node = _self.get(&#39;node&#39;);
    $(node).delegate(selector,eventType,fn);
    return this;
  },*/
  //获取属性值
  _getAttr : function(name){
    var _self = this,
      el = _self.get(&#39;el&#39;),
      value = el.attr ? el.attr(name) : &#39;&#39;,
      m = &#39;__get&#39; + BUI.ucfirst(name);
    if(_self[m]){
      value = _self[m](value);
    }
    return value;
  },
  //设置属性值
  _setAttr : function(name,value){
    var _self = this,
      el = _self.get(&#39;el&#39;),
      m = &#39;__set&#39; + BUI.ucfirst(name);
    if(_self[m]){
      _self[m](value);
    }else{
      el.attr &amp;&amp; el.attr(name,value);
    }
    return _self;
  },
<span id='BUI-Graphic-Base-method-beforeRenderUI'>  /**
</span>   * @protected
   * 渲染控件
   */
  beforeRenderUI : function(){

  },
<span id='BUI-Graphic-Base-method-render'>  /**
</span>   * 渲染控件/图形
   */
  render : function(){
    var _self = this,
      cls = _self.get(&#39;elCls&#39;),
      zIndex = _self.get(&#39;zIndex&#39;),
      node;

    if(!_self.get(&#39;rendered&#39;)){
          _self.beforeRenderUI();

      _self.renderUI();
      _self.set(&#39;rendered&#39;,true);
          node = _self.get(&#39;node&#39;);
          if(this.get(&#39;visible&#39;) == false){
              this.hide();
          }
          if(cls){
              var oldCls = node.getAttribute(&#39;class&#39;);
                  
              if(oldCls){
                 node.setAttribute(&#39;class&#39;,oldCls + &#39; &#39; + cls); 
              }else{
                 node.setAttribute(&#39;class&#39;,cls); 
              }
              
          }
          if(zIndex != null){
              node.setAttribute(&#39;zIndex&#39;,zIndex);
              if(Util.vml){
                  $(node).css(&#39;z-index&#39;,zIndex);
              }
          }
          _self.bindUI();
    }
  },
<span id='BUI-Graphic-Base-method-renderUI'>  /**
</span>   * @protected
   * 渲染控件
   */
  renderUI : function(){

  },
<span id='BUI-Graphic-Base-method-bindUI'>  /**
</span>   * @protected
   * 绑定事件
   */
  bindUI : function(){

  },
<span id='BUI-Graphic-Base-method-remove'>  /**
</span>   * 移除，从父元素中移除
   * @param  {Boolean} [destroy=true] 
   */
  remove : function(destroy){
    if(destroy == undefined){
      destroy = true;
    }
    var _self = this;
    if(_self.get(&#39;parent&#39;)){
      _self.get(&#39;parent&#39;).removeChild(_self,destroy);
          _self.set(&#39;parent&#39;,null);
    }else if(destroy){
      _self.destroy();
    }
  },
<span id='BUI-Graphic-Base-method-destroy'>  /**
</span>   * 析构函数
   */
  destroy : function(){
    var _self = this,
      el = _self.get(&#39;el&#39;),
          destroyed = _self.get(&#39;destroyed&#39;),
          node = _self.get(&#39;node&#39;);
      if(destroyed){
          return;
      }
    el.remove &amp;&amp; el.remove();
    _self._attrs = {};
      $(node).off();
      _self.set(&#39;destroyed&#39;,true);
  }

});
  
module.exports = Base;
</pre>
</body>
</html>
